package selfview.sufutian.com.myapplication.draw2d;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.graphics.RectF;

import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;

/**
 * Created by sufutian on 2017/7/27.
 */

public class PathMeasureApiView extends View {

    private Paint mPaint;

    private float bigCurrent, smallCurrent, lineCurrent;
    private float[] smallPos;
    private float[] smalltan;
    private float[] bigPos;
    private float[] bigtan;
    private Path smallPath;
    private Path bigPath;
    private PathMeasure smallMeasure;
    private PathMeasure bigMeasure;
    private Path linePath;
    private Path splitePath, splitePath2, splitePath3;

    private PathMeasure linePathMeasure, smallPeasure;

    boolean bigAnmiEnd;


    public PathMeasureApiView(Context context) {
        super(context);
        init();
    }

    public PathMeasureApiView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(5);

        smallPath = new Path();
        RectF oval1 = new RectF(-50, -50, 50, 50);          // 放大镜圆环
        smallPath.addArc(oval1, 45, -359.9f);
        smallPos = new float[2];
        smalltan = new float[2];

        bigPath = new Path();
        RectF oval2 = new RectF(-150, -150, 150, 150);          // 放大镜圆环
        bigPath.addArc(oval2, 45, -359.9f);
        bigPos = new float[2];
        bigtan = new float[2];

        smallMeasure = new PathMeasure();
        smallMeasure.setPath(smallPath, true);
        smallMeasure.getPosTan(0, smallPos, smalltan);

        bigMeasure = new PathMeasure();
        bigMeasure.setPath(bigPath, true);
        bigMeasure.getPosTan(0, bigPos, bigtan);

        Log.e("sufutian==", "smallPos: " + smallPos[0] + "--" + smallPos[1]);
        Log.e("sufutian==", "bigPos: " + bigPos[0] + "--" + bigPos[1]);


        linePath = new Path();
        linePath.moveTo(bigPos[0], bigPos[1]);
        linePath.lineTo(smallPos[0], smallPos[1]);
        linePath.close();


        splitePath = new Path();
        splitePath2 = new Path();
        splitePath3 = new Path();

        linePathMeasure = new PathMeasure();
        linePathMeasure.setPath(linePath, false);

        smallPeasure = new PathMeasure();
        smallPeasure.setPath(smallPath, false);


        final ValueAnimator smalltobig = ValueAnimator.ofFloat(0, 1);
        smalltobig.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                bigCurrent = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
        smalltobig.setDuration(1000);


        final ValueAnimator lineAnmi = ValueAnimator.ofFloat(0, 1);
        lineAnmi.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                lineCurrent = (float) animation.getAnimatedValue();
                Log.e("sufutian", "lineCurrent: " + lineCurrent);
                invalidate();
            }
        });
        lineAnmi.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                smalltobig.start();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        lineAnmi.setDuration(1000);
        lineAnmi.setInterpolator(new LinearInterpolator());


        ValueAnimator bigAnmi = ValueAnimator.ofFloat(0, 1);
        bigAnmi.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                smallCurrent = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
        bigAnmi.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                lineAnmi.start();
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        bigAnmi.setDuration(2000);
        bigAnmi.setRepeatCount(2);
        bigAnmi.start();


//        ValueAnimator bigtosmall = ValueAnimator.ofFloat(1, 0);
//        bigtosmall.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                smallCurrent = (float) animation.getAnimatedValue();
//                invalidate();
//            }
//        });
//        bigtosmall.setDuration(2000);
//        bigtosmall.setRepeatCount(ValueAnimator.INFINITE);
//        bigtosmall.start();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(500, 500);

        if (!bigAnmiEnd) {
            splitePath3.reset();
            float stop3 = bigMeasure.getLength() * smallCurrent;
            float start = (float) (stop3 - ((0.5 - Math.abs(smallCurrent - 0.5)) * (bigMeasure.getLength()) / 2));
            bigMeasure.getSegment(start, stop3, splitePath3, true);
            canvas.drawPath(splitePath3, mPaint);
        }

        splitePath.reset();
        // 硬件加速的BUG
        //path片段绘制
        float stop = lineCurrent * linePathMeasure.getLength();
        linePathMeasure.getSegment(0, stop, splitePath, true);
        canvas.drawPath(splitePath, mPaint);


        splitePath2.reset();
        float stop2 = bigCurrent * smallMeasure.getLength();
        smallMeasure.getSegment(0, stop2, splitePath2, true);
        canvas.drawPath(splitePath2, mPaint);


    }
}
